package com.lqian.tcp;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

    public static void main(String[] args) {
        //1.创建客户端的socket,指定ip and port
        //2.获取该socket流，向服务端发送数据
        //3.获取输入流，取得服务端的数据

        // 1.创建客户端的Socket，指定服务器的IP和端口
        try {
            Socket socket = new Socket("127.0.0.1", 9999);

            // 2.获取该Socket的输出流，用来向服务器发送信息
            OutputStream os = socket.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(new User("mic", "123456"));
            socket.shutdownOutput();

            // 3.获取输入流，取得服务器的信息
            InputStream is = socket.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            String info=null;
            while((info=br.readLine())!=null){
                System.out.println("服务器端的信息：" + info);
            }
            socket.shutdownInput();
            oos.close();
            os.close();
            is.close();
            br.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
